package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.cardmanagement.util.SaleCardStockDetail;
import com.turing.post.cardmanagement.util.SaleCardStockDetailDao;
import com.turing.post.cardmanagement.util.TransferInOutStockOrderDetail;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "cardmanagement.impl.SaleCardStockDetailDaoImpl")
@Remote
@Local
public class SaleCardStockDetailDaoImpl implements SaleCardStockDetailDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	private static final String grpSumSql = ""
	// 调拨入库
			+ "sum(case when t.tranType = '1' then t.orderCount else 0 end), "
			// 调拨出库
			+ "sum(case when t.tranType = '2' then t.orderCount else 0 end), "
			// 盘盈入库
			+ "sum(case when t.tranType = '3' then t.orderCount else 0 end), "
			// 销售出库
			+ "sum(case when t.tranType = '4' then t.orderCount else 0 end), "
			// 补换发出库
			+ "sum(case when t.tranType = '5' then t.orderCount else 0 end), "
			// 丢失损坏出库
			+ "sum(case when t.tranType = '9' then t.orderCount else 0 end) ";

	@Override
	public Integer getSaleCardStockDetailCount(String SaleBranchNo,
			String batchNo, String starttime, String endtime, String TranType,
			BigInteger cardFaceId, String startSeqNo, String endSeqNo) {
		String sql = "select nvl(count(*),0) from SaleCardStockDetail "
				+ "where (?1 is null or SaleBranchNo=?1) "
				+ "and (?2 is null or batchNo=?2) "
				+ "and Trantime >= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ "and Trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss') "
				+ "and (?5 is null or TranType=?5) "
				+ "and (?6 is null or cardFaceId=?6) "
				+ "and (?7 is null or ?7 between cardstartseqno and cardendseqno) "
				+ "and (?8 is null or ?8 between cardstartseqno and cardendseqno) ";

		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, SaleBranchNo);
		query.setParameter(2, batchNo);
		query.setParameter(3, starttime);
		query.setParameter(4, endtime);
		query.setParameter(5, TranType);
		query.setParameter(6, cardFaceId);
		query.setParameter(7, startSeqNo);
		query.setParameter(8, endSeqNo);
		return Integer.parseInt(query.getSingleResult().toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SaleCardStockDetail> getSaleCardStockDetailList(
			String SaleBranchNo, String batchNo, String starttime,
			String endtime, String TranType, BigInteger cardFaceId,
			String startSeqNo, String endSeqNo, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from SaleCardStockDetail "
				+ " where (?1 is null or SaleBranchNo=?1) "
				+ " and (?2 is null or batchNo=?2) "
				+ " and Trantime >= to_date(?3,'yyyy-mm-dd hh24:mi:ss') "
				+ " and Trantime<=to_date(?4,'yyyy-mm-dd hh24:mi:ss') "
				+ " and (?5 is null or TranType=?5) "
				+ " and (?6 is null or cardFaceId=?6) "
				+ " and (?7 is null or ?7 between cardstartseqno and cardendseqno) "
				+ " and (?8 is null or ?8 between cardstartseqno and cardendseqno) "
				+ " order by TranTime desc";

		Query query = _em.createNativeQuery(sql, SaleCardStockDetail.class);
		query.setFirstResult(startPosition);
		query.setMaxResults(maxResult);
		query.setParameter(1, SaleBranchNo);
		query.setParameter(2, batchNo);
		query.setParameter(3, starttime);
		query.setParameter(4, endtime);
		query.setParameter(5, TranType);
		query.setParameter(6, cardFaceId);
		query.setParameter(7, startSeqNo);
		query.setParameter(8, endSeqNo);
		return query.getResultList();
	}

	@Override
	public void insert(SaleCardStockDetail SaleCardStockDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, SaleCardStockDetail);
	}

	/**
	 * 按销售机构号分类汇总某发卡商某日某销售商下属销售机构记帐流水
	 * 
	 * @param issueCompanyNo
	 *            发卡商NO
	 * @param tranDate
	 *            帐务日期
	 * @param issueBranchNo
	 *            发卡商分支机构
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> sumAllSaleCardStockDaySettlementByBranch(
			String issueCompanyNo, Date tranDate, String issueBranchNo) {
		Query query = _em
				.createNativeQuery("select t.saleBranchNo, t.cardFaceId, "
						+ grpSumSql
						+ "from SaleCardStockDetail t, SysBranchSubord s "
						+ "where t.saleBranchNo like ?1 and t.tranDate = ?2 and "
						+ "t.saleBranchNo = s.SubBranchNo and s.BranchNo = ?3 and s.SubBranchType = '12' "
						+ "group by t.saleBranchNo, t.cardFaceId ");
		query.setParameter(1, issueCompanyNo + "%");
		query.setParameter(2, tranDate);
		query.setParameter(3, issueBranchNo);
		List<Object[]> obj_list = query.getResultList();
		if (obj_list == null)
			obj_list = null;
		return obj_list;
	}

	/**
	 * @根据IoOrderId,IssueBranchNo,TranDate查询明细
	 * @param
	 * @return 对象
	 */
	@Override
	public SaleCardStockDetail getSaleCardStockDetailByDatil(
			BigInteger IoOrderId, String IssueBranchNo, Date TranDate) { // TODO
		// Auto-generated
		// method
		// stub
		String sql = "select * from SaleCardStockDetail where IoOrderId="
				+ IoOrderId + " and and TranDate = to_date('" + TranDate
				+ "','yyyy-mm-dd')" + " and SaleBranchNo='" + IssueBranchNo
				+ "'";
		Query query = _em.createNativeQuery(sql,
				TransferInOutStockOrderDetail.class);
		return (SaleCardStockDetail) query.getSingleResult();
	}

	@SuppressWarnings("unchecked")
	public List<Object[]> getSaleCardStockDetailByCondition(
			PCPSWebInput webInput, String saleBranchNo, String startDate,
			String endDate) {
		if (webInput == null || webInput.getOperId() == null)
			return null;
		String sql = "select c.salebranchname, b.cardfacename,a.SaleBranchNo,a.OrderCount,a.CardStartSeqNo,a.CardEndSeqNo,a.TranDate,a.remark from SaleCardStockDetail a,CardFace b,SaleBranch c "
				+ " where a.TranType = '4' and a.cardfaceid=b.cardfaceid  and a.salebranchno=c.salebranchno "
				+ " and a.TranDate >= to_date(?1,'yyyy-mm-dd') "
				+ " and a.TranDate <= to_date(?2,'yyyy-mm-dd')";
		sql += PCPSUtil.getSqlCond(webInput, saleBranchNo, "a.SaleBranchNo");
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, startDate);
		query.setParameter(2, endDate);
		List<Object[]> list = query.getResultList();
		return list;
	}
}
